package cookie;

import cookie.swing.CheckGUI;
import cookie.swing.MakeGUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;

/**
 * Created by IntelliJ IDEA.
 * User: Nathaniel
 * Date: 12/10/11
 * Time: 3:30 PM
 */

public class Main extends JFrame {
    private File file;
    
    public static void main(String[] args){
        new Main().setVisible(true);
    }
    
    private Vector<String> vector = new Vector<String>();

    private File dir = new File(System.getenv("USERPROFILE") + File.separator + "Apex" + File.separator + "Homework" + File.separator + "Lists");

    public Main() {
        listPane = new JScrollPane();
        list = new JList();
        addBtn = new JButton();
        removeBtn = new JButton();
        checkBtn = new JButton();
        separator = new JSeparator();

        if(!dir.exists()){
            dir.mkdirs();
        }

        for(String s : dir.list()){
            vector.add(s.replace(".xml", ""));
        }

        if(vector != null){
            list.setListData(vector);
        }

        setTitle("Apex Homework");
        Container contentPane = getContentPane();

        listPane.setViewportView(list);

        addBtn.setText("Add");
        addBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    MakeGUI g = new MakeGUI();
                    g.setVisible(true);
                    dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        removeBtn.setText("Remove");
        removeBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    String name = (String) list.getSelectedValue();
                    File f = new File(dir.getPath() + File.separator + name + ".xml");
                    if(f.exists()){
                        f.delete();
                    }
                    vector.remove(list.getSelectedIndex());
                    list.setListData(vector);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        checkBtn.setText("Check");
        checkBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if(list.getSelectedValue() != null){
                        String name = (String) list.getSelectedValue();
                        file = new File(dir.getPath() + File.separator + name + ".xml");
                        if(file.exists()){
                            CheckGUI g = new CheckGUI(file);
                            g.setVisible(true);
                            dispose();
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Please choose a list", "Apex Homework", JOptionPane.PLAIN_MESSAGE);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(listPane, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                .addComponent(addBtn, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(removeBtn, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addComponent(separator, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                                        .addComponent(checkBtn, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                                .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(listPane, GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                        .addGroup(GroupLayout.Alignment.LEADING, contentPaneLayout.createSequentialGroup()
                                                .addComponent(addBtn)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(removeBtn)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(separator, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(checkBtn)))
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }

    private JScrollPane listPane;
    private JList list;
    private JButton addBtn;
    private JButton removeBtn;
    private JButton checkBtn;
    private JSeparator separator;
}
